package com.delong.lease.web.app.service.impl;

import com.delong.lease.common.exception.LeaseException;
import com.delong.lease.common.result.ResultCodeEnum;
import com.delong.lease.model.entity.*;
import com.delong.lease.model.enums.ItemType;
import com.delong.lease.model.enums.LeaseStatus;
import com.delong.lease.model.enums.ReleaseStatus;
import com.delong.lease.web.app.mapper.*;
import com.delong.lease.web.app.service.ApartmentInfoService;
import com.delong.lease.web.app.service.BrowsingHistoryService;
import com.delong.lease.web.app.service.RoomInfoService;
import com.delong.lease.web.app.vo.apartment.ApartmentItemVo;
import com.delong.lease.web.app.vo.attr.AttrValueVo;
import com.delong.lease.web.app.vo.fee.FeeValueVo;
import com.delong.lease.web.app.vo.graph.GraphVo;
import com.delong.lease.web.app.vo.room.RoomDetailVo;
import com.delong.lease.web.app.vo.room.RoomItemVo;
import com.delong.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;
    @Override
    public void pageList(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        roomInfoMapper.pageList(roomItemVoPage,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id, Long userId) {

        //自定义根据id查询room信息，否则查询房间是否删除的时候查不到，封装好的方法自动查未删除的
        RoomInfo roomInfo = roomInfoMapper.selectRoomById(id);
        if (roomInfo == null){
            throw new LeaseException(ResultCodeEnum.DATA_ERROR);
        }
        Long apartmentId = roomInfo.getApartmentId();
        //1、查询公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentInfoById(apartmentId);
        //2、查询图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoById(ItemType.ROOM,id);
        //3、查询属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueById(id);
        //4、查询配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoById(id);
        //5、查询标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoByRoomId(id);
        //6、查询支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentTypeByRoomId(id);
        //7、查询杂费列表
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueById(apartmentId);
        //8、查询租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermById(id);
        //9、房间是否被删除
        Boolean isDeleted = roomInfo.getIsDeleted() == 1;
        //10、房间是否已入住
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getRoomId, roomInfo.getId());
        leaseAgreementLambdaQueryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING, LeaseStatus.RENEWING);
        Long selectCount = leaseAgreementMapper.selectCount(leaseAgreementLambdaQueryWrapper);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsDelete(isDeleted);
        roomDetailVo.setIsCheckIn(selectCount > 0);
        //保存浏览记录

        browsingHistoryService.saveBrowsingHistory(userId, id);

        return roomDetailVo;
    }

    @Override
    public void pageListByApartId(Page<RoomItemVo> roomItemVoPage, Long id) {
        roomItemVoPage = roomInfoMapper.pageListByApartId(roomItemVoPage, id);
    }
}




